﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Week1.world.behavior
{
    class BehaviorMoveStraight:BehaviorBase
    {
        private Direction[][] oDirection;
        private float[][] fSpeed;

        public enum Direction
        {
            North, South, East, West
        }

        private BehaviorMoveStraight()
            : base()
        {
            oDirection = new Direction[MAXREFERENCES][];
            fSpeed = new float[MAXREFERENCES][];
        }

        private static BehaviorMoveStraight instance;

        public static BehaviorMoveStraight Instance
        {
            get
            {
                if (instance == null)
                    instance = new BehaviorMoveStraight();

                return instance;
            }
        }

        public override int getBehaviorHandlerID(DrawableGlobalVar oVars)
        {
            oDirection[iHandlerIDIndex] = new Direction[oVars.iInstanceCount];
            fSpeed[iHandlerIDIndex] = new float[oVars.iInstanceCount];
            return base.getBehaviorHandlerID(oVars);
        }

        public override void execute(int handlerid, DrawableGlobalVar oVars, GameTime gameTime)
        {
            if (isTriggerActive(handlerid))
            {
                unsafe
                {
                    for (int iDx = 0; iDx < oVars.oPosition.Length; iDx++)
                    {
                        switch (oDirection[handlerid][iDx])
                        {
                            case Direction.East:
                                oVars.oPosition[iDx].X += fSpeed[handlerid][iDx];
                                break;
                            case Direction.West:
                                oVars.oPosition[iDx].X -= fSpeed[handlerid][iDx];
                                break;
                            case Direction.North:
                                oVars.oPosition[iDx].Z -= fSpeed[handlerid][iDx];
                                break;
                            case Direction.South:
                                oVars.oPosition[iDx].Z += fSpeed[handlerid][iDx];
                                break;
                        }

                        oVars.oWorld[iDx].M41 = oVars.oPosition[iDx].X;
                        oVars.oWorld[iDx].M42 = oVars.oPosition[iDx].Y;
                        oVars.oWorld[iDx].M43 = oVars.oPosition[iDx].Z;
                    }
                }
            }
        }

        public override void initialize(int handlerid, DrawableGlobalVar oVars)
        {
            for (int iDx = 0; iDx < oVars.iInstanceCount; iDx++)
            {
                oVars.oWorld[iDx] = Matrix.CreateWorld(oVars.oPosition[iDx], Vector3.Forward, Vector3.Up);
            }
        }

        /// <summary>
        /// sets the direction and speed.
        /// </summary>
        /// <param name="handler"></param>
        /// <param name="instanceIndex"></param>
        /// <param name="name">north, south, east, west</param>
        /// <param name="value">speed float value</param>
        public override void setBehaviorProperty(int handler, int instanceIndex, string name, float value)
        {
            if (name.Equals("north"))
                oDirection[handler][instanceIndex] = Direction.North;
            else if (name.Equals("south"))
                oDirection[handler][instanceIndex] = Direction.South;
            else if (name.Equals("east"))
                oDirection[handler][instanceIndex] = Direction.East;
            else if (name.Equals("west"))
                oDirection[handler][instanceIndex] = Direction.West;

            fSpeed[handler][instanceIndex] = value;
        }
     }
}
